<?php 

namespace app\admin\controller;
use app\common\model\Shop as ShopModel;
use app\admin\controller\Base;
use app\admin\validate\ShopValidate;
use app\admin\event\ShopEvent;
use app\common\utils\JsonService;
use think\facade\View;

class Shop extends Base{


    /**
    * 编辑店铺
    * @return mixed
    */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new ShopValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return ShopEvent::updateShop($post);
        }

        $shop_id = $this->request->get('shop_id');
        $model = $this->findModel($shop_id);
        $shop_group = \app\common\model\ShopGroup::getAll();
        return View::fetch('update',[
            'model'=>$model,
            'shop_group'=>$shop_group,
        ]);
    }

    /**
    * 店铺列表

    * @return mixed
    */
    public function index(){
        $dataProvider = ShopEvent::shopList();
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }



    /**
    * 得到模型
    *
    * @param int $id
    * @param array $where
    *
    * @return array|false|\PDOStatement|string|\think\Model
    */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return ShopModel::findModel($id, $where);
    }

}
